package com.domain.service;

import com.domain.Movies;

import com.domain.utils.GetConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class MoviesService {

    private Connection connection = null;
    private Statement statement = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    public void create(Movies movies) {


        try {

            connection = GetConnection.setConnection();

            preparedStatement = connection.prepareStatement("INSERT INTO MOVIES VALUES(?,?,?,?,?)");

            preparedStatement.setString(1, movies.getLanguage());
            preparedStatement.setString(2, movies.getCategory());
            preparedStatement.setString(3, movies.getDescription());
            preparedStatement.setString(4, movies.getMovieName());
            preparedStatement.setString(5, movies.getYear());

            preparedStatement.executeUpdate();

            GetConnection.closeConnection();

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }


    }

    public List<Movies> getAll() {


        List<Movies> moviesList = null;

        try {
            connection = GetConnection.setConnection();

            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM MOVIES");

            moviesList = new ArrayList<Movies>();

            while (resultSet.next()) {

                Movies movies = new Movies();

                movies.setLanguage(resultSet.getString(1));
                movies.setCategory(resultSet.getString(2));
                movies.setDescription(resultSet.getString(3));
                movies.setMovieName(resultSet.getString(4));
                movies.setYear(resultSet.getString(5));

                moviesList.add(movies);
            }

            GetConnection.closeConnection();

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }

        }


        return moviesList;
    }
}
